﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Practices.ServiceLocation;
using System.ComponentModel.Composition.Hosting;
using System.IO;

namespace Dynamic_UI_Framework
{
    /// <summary>
    /// ToolBar.xaml 的交互逻辑
    /// </summary>
    public partial class ToolBar : Window
    {
        public ToolBar()
        {
            InitializeComponent();

            ElementController ec = ServiceLocator.Current.GetInstance<ElementController>();
            this.lbElements.DisplayMemberPath = "ContractName";
            this.lbElements.SelectedValuePath = "ExportTypeIdentity";
            this.lbElements.ItemsSource = ec.Nodes;


            PartController pc = ServiceLocator.Current.GetInstance<PartController>();
            if (pc.Root != null)
            {
                foreach (var item in pc.Root.ChildNodes)
                {
                    TreeViewItem ti = new TreeViewItem { Tag = item, Header = Path.GetFileNameWithoutExtension(item.Path) };
                    this.tvParts.Items.Add(ti);
                    if (item.IsFolder)
                    {
                        DoBuildTreeView(ti);
                    }
                }
            }

            RegionAdapterMapping mapping = ServiceLocator.Current.GetInstance<RegionAdapterMapping>();
            foreach (var m in mapping.Mappings)
            {
                MenuItem item = new MenuItem { Tag = m.Key, Header = m.Key.Name};
                item.Click += new RoutedEventHandler(MenuItem_Click);
                this.menuRegion.Items.Add(item);
            }

            RegionController rc = ServiceLocator.Current.GetInstance<RegionController>();
            this.lbRegions.DisplayMemberPath = "Name";
            this.lbRegions.ItemsSource = null;
            this.lbRegions.ItemsSource = rc.Regions;

            UITree tree = ServiceLocator.Current.GetInstance<UITree>();
            this.tvUITree.Items.Add(tree.GetTreeViewItem());
            UIContext.VisualModified += (s, e) => {
                UITree t = ServiceLocator.Current.GetInstance<UITree>();
                this.tvUITree.Items.Clear();
                this.tvUITree.Items.Add(t.GetTreeViewItem());

            };

            this.tvUITree.SelectedItemChanged += (s, e) => {
                UIContext.DesignerVisual = tree.Root;
                TreeViewItem item = e.NewValue as TreeViewItem;
                if (item == null)
                    return;
                UINode node = item.Tag as UINode;
                node.Target.Activate(null);
            };
        }

        private void DoBuildTreeView(TreeViewItem item)
        {
           PartController.PartNode  node = item.Tag as PartController.PartNode;
           if (node.IsFolder == false)
               return;

           foreach (var n in node.ChildNodes)
           {
               TreeViewItem ti = new TreeViewItem { Tag = n, Header = Path.GetFileNameWithoutExtension(n.Path) };
               item.Items.Add(ti);
               if (n.IsFolder)
                   DoBuildTreeView(ti);
           }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            RegionAdapterMapping mapping = ServiceLocator.Current.GetInstance<RegionAdapterMapping>();
            IAdapter adapter = mapping.GetMapping((sender as MenuItem).Tag as Type);
            RegionController rc = ServiceLocator.Current.GetInstance<RegionController>();
            if (adapter != null)
            {
                rc.CreateRegion(adapter);
            }
            this.lbRegions.DisplayMemberPath = "Name";
            this.lbRegions.ItemsSource = null;
            this.lbRegions.ItemsSource = rc.Regions;
        }

        private void lbRegions_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ListBox lb = sender as ListBox;
            object selectedItem = lb.SelectedItem;
            if (selectedItem != null)
            {
                RegionController rc = ServiceLocator.Current.GetInstance<RegionController>();
                rc.CurrentRegion = selectedItem as IRegion;
                UIContext.DesignerVisual = rc.CurrentRegion;
            }
        }

        private void btnSaveUITree_Click(object sender, RoutedEventArgs e)
        {
            UITree tree = ServiceLocator.Current.GetInstance<UITree>();
            tree.Save();
        }
    }
}
